package cn.snowheart

import groovy.xml.MarkupBuilder

/**
 * TODO Xml 生成与解析
 *
 * @author liuwanxiang
 * @version 2019/05/31
 */
class XmlExample {

    static void main(String[] args) {
        // 生成
        generate()

        // 解析
        // parse()
    }

    /**
     * 打印生成出来的xml文档
     */
    static void generate() {
        def mp = [1: ['Enemy Behind', 'War, Thriller', 'DVD', '2003',
                      'PG', '10', 'Talk about a US-Japan war'],
                  2: ['Transformers', 'Anime, Science Fiction', 'DVD', '1989',
                      'R', '8', 'A scientific fiction'],
                  3: ['Trigun', 'Anime, Action', 'DVD', '1986',
                      'PG', '10', 'Vash the Stam pede'],
                  4: ['Ishtar', 'Comedy', 'VHS', '1987', 'PG',
                      '2', 'Viewable boredom ']]

        def mb = new MarkupBuilder()

        def MOVIEDB = mb.collection('shelf': 'New Arrivals') {
            mp.each {
                sd ->
                    mb.movie('title': sd.value[0]) {
                        type(sd.value[1])
                        format(sd.value[2])
                        year(sd.value[3])
                        rating(sd.value[4])
                        stars(sd.value[4])
                        description(sd.value[5])
                    }
            }
        }
    }

    /**
     * 根据xml文件解析并打印
     */
    static void parse() {
        def parser = new XmlParser()
        def doc = parser.parse(new File("E:\\movie.xml"))

        doc.movie.each {
            println "Movie Name: ${it['@title']}"
            println "Movie Type: ${it.type[0].text()}"
            println "Movie Format: ${it.format[0].text()}"
            println "Movie Year: ${it.year[0].text()}"
            println "Movie Rating: ${it.rating[0].text()}"
            println "Movie Stars: ${it.stars[0].text()}"
            println "Movie Description: ${it.description[0].text()}"
            println "*****************************"
        }
    }

}
